package com.zuxia.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.zuxia.dao.IStockDao;
import com.zuxia.entity.Acmodel;
import com.zuxia.entity.Car;
import com.zuxia.entity.Stock;
/**
 *���־ò�ʵ���� 
 */
@Repository("stockDao")
public class StockDaoImpl implements IStockDao {
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	/**
	 *��� 
	 */
	public int saveStock(Stock s) {
		int i = (int) hibernateTemplate.save(s);
		return i;
	}
	/**
	 * ��ѯ����
	 */
	@SuppressWarnings("unchecked")
	public List<Car> findCarModel() {
		return (List<Car>) hibernateTemplate.find("from Car");
	}
	/**
	 * ��ѯ����ͺ�
	 */
	@SuppressWarnings("unchecked")
	public List<Acmodel> findModel() {
		return (List<Acmodel>) hibernateTemplate.find("from Acmodel");
	}
	/**
	 * ��ѯȫ��
	 */
	@SuppressWarnings("unchecked")
	public List<Stock> findAll() {
		return (List<Stock>) hibernateTemplate.find("from Stock");
	}
	/**
	 *��ѯ�������� 
	 */
	public Stock findStockById(Stock stock) {
		return hibernateTemplate.get(stock.getClass(), stock.getStockId());
	}
	/**
	 *�޸� 
	 */
	public int updateStock(Stock stock) {
		try {
			hibernateTemplate.update(stock);
			return 1;
		} catch (Exception e) {
			return 0;
		}
	}
	/**
	 * ɾ��
	 */
	public int deleteStock(Stock stock) {
		try {
			Stock s = hibernateTemplate.get(stock.getClass(), stock.getStockId());
			hibernateTemplate.delete(s);
		} catch (Exception e) {
			System.out.println("ɾ��ʧ�ܣ�����");
			return 0;
		}
		return 1;
	}
	/**
	 * ����ɾ��
	 */
	public void batchDelteStock(String  stchecks) {
		String[] split = stchecks.split(",");
		for (String s : split) {
			if(s!=null&&s!="") {
				hibernateTemplate.delete(hibernateTemplate.get(Stock.class,Integer.parseInt(s)));
			}
		}
	}
	
	public int CountStock() {
		List<Stock> list = (List<Stock>) hibernateTemplate.find("from Stock");
		return list.size();
	}
	
	public List<Stock> findPageStokc(int firstResult, int maxResults, DetachedCriteria dCriteria) {
		return (List<Stock>) hibernateTemplate.findByCriteria(dCriteria, firstResult, maxResults);
	}
	
}
